package com.sakiko.ioc;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.*;

public class DruidDemo {
    public static void main(String[] args) throws SQLException {
        // 1、创建连接池  [加入到 ioc容器]
        DruidDataSource dDs = new DruidDataSource();
        dDs.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dDs.setUrl("jdbc:mysql:///delivery_system");
        dDs.setUsername("root");
        dDs.setPassword("abc123");
        // 2、获取连接
        Connection connection = dDs.getConnection();
        // 3、数据库 CRUD
        String sql = "select * from sys_user;";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        // metaData 包含列的信息 列的数量: ColumnCount 列名: ColumnName
        ResultSetMetaData metaData = resultSet.getMetaData();
        int columnCount = metaData.getColumnCount();
        while (resultSet.next()) {
            for (int i = 1; i <= columnCount; i++) {
                String value = resultSet.getString(i); // ColumnIndex or ColumnLabel
                String columnName = metaData.getColumnName(i);
                System.out.println(columnName + "::" + value + " ");
            }
            System.out.println("-----------------------");
        }
        // 4、释放资源
        resultSet.close();
        preparedStatement.close();
        connection.close();
        dDs.close();
    }
}
